<template>
	<view>
		<view class="uni-form-item uni-column">
			<view class="title"><text class="uni-form-item__title">学校编号</text></view>
			<view class="uni-input-wrapper">
				<input class="uni-input" maxlength="20" placeholder="请输入学校编号" v-model="schoolCode" />
			</view>
			<label v-for="(value, name, index) in errormsgs" v-if="name=='Entity.SchoolCode'" class="color-red">
				{{value}}
			</label>
		</view>
		<view class="uni-form-item uni-column">
			<view class="title"><text class="uni-form-item__title">学校名称</text></view>
			<view class="uni-input-wrapper">
				<input class="uni-input" maxlength="20" placeholder="请输入学校名称" v-model="schoolName" />
			</view>
			<label v-for="(value, name, index) in errormsgs" v-if="name=='Entity.SchoolName'" class="color-red">
				{{value}}
			</label>
		</view>
		<view class="uni-list">
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					学校类型
				</view>
				<view class="uni-list-cell-db">
					<picker @change="bindPickerChange" :value="index" :range="array" range-key="name">
						<view class="uni-input">{{array[index].name}}</view>
					</picker>
				</view>
			</view>
		</view>
		<view class="uni-form-item uni-column">
			<view class="title"><text class="uni-form-item__title">学校备注</text></view>
			<view class="uni-input-wrapper">
				<input class="uni-input" maxlength="20" placeholder="请输入学校备注" v-model="Remark" />
			</view>
			<label v-for="(value, name, index) in errormsgs" v-if="name=='Entity.Remark'" class="color-red">
				{{value}}
			</label>
		</view>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="button-sp-area">
				<button v-if="isUpdate==0" type="primary" plain="true" @click="SchoolAdd()">添加</button>
				<button v-else type="primary" plain="true" @click="SchoolUpdate()">修改</button>
			</view>
		</view>
	</view>
</template>

<script>
	import schoolservice from '../../myservice/schoolservice.js'
	import ajaxservice from '../../myservice/ajaxservice.js'

	export default {
		data() {
			return {
				schoolCode: "",
				schoolName: "",
				schoolType: "PUB",
				Remark: "",
				isUpdate: 0, 
				schoolId: 0,
				errormsgs: [],
				index: 0,
				array: [{type:"PUB",name:"公立学校"},{type:"PRI",name:"私立学校"}]//{"PUB":"公立学校","PRI":"私立学校"}
			}
		},
		methods: {
			SchoolAdd: function() { //添加
				var that = this;
				schoolservice.SchoolAdd({
					Entity: {
						SchoolCode: that.schoolCode,
						SchoolName: that.schoolName,
						SchoolType: that.schoolType,
						Remark: that.Remark,
					}
				}).then(res => {
					console.log(JSON.stringify(res));
					if (res.statusCode == 200) {
						uni.showToast({
							title: '添加成功',
							icon: 'none'
						});
						//setTimeout(function() {}, 1000);
						uni.switchTab({
							url: './schoollist'
						})
					} else {
						//if(res.data)
						that.errormsgs = res.data.Form;
						if(res.data.Message.length>0){
							uni.showToast({
								title: res.data.Message[0]
							});
						}
					}
				})
			},
			SchoolUpdate: function() { //修改
				var that = this;
				schoolservice.SchoolEdit({
					Entity: {
						SchoolCode: that.schoolCode,
						SchoolName: that.schoolName,
						SchoolType: that.schoolType,
						Remark: that.Remark,
						ID: that.schoolId
					}
				}).then(res => {
					console.log(JSON.stringify(res));
					if (res.statusCode == 200) {
						uni.showToast({
							title: '修改成功',
							icon: 'none'
						});
						//setTimeout(function() {}, 1000);
						uni.switchTab({
							url: './schoollist'
						})
					} else {
						that.errormsgs = res.data.Form;
						if(res.data.Message.length>0){
							uni.showToast({
								title: that.data.Message[0]
							});
						}
					}
				})
			},
			bindPickerChange(e){
				 console.log(JSON.stringify(e))
				 this.index=e.detail.value;
				 this.schoolType=this.array[this.index].type;
				 console.log(e.detail.value)
			}
			
		},
		onLoad(option) {
			var that = this;
			that.schoolId = option.schoolId;
			console.log(option.schoolId)
			if (that.schoolId > 0) {
				that.isUpdate = 1;
				console.log(that.isUpdate)
				schoolservice.SchoolGet(that.schoolId).then(res => {
					console.log(JSON.stringify(res));
					var entity = res.data.Entity;
					that.schoolCode = entity.SchoolCode;
					that.schoolName = entity.SchoolName;
					that.schoolType = entity.SchoolType;
					that.Remark = entity.Remark;
				});
			}
		}
	}
</script>

<style>
	.color-red{
		color: red;
	}
</style>
